<?php

class Technical_TypestudyController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
    }

    public function indexAction()
    {
        $typeStudys = new Application_Model_Dao_TypeStudy();
        $result = $typeStudys->getAll()->toArray();
        $this->view->typeStudies = $result;
    	if($result == null){
			$this->view->empty = "Brak rodzaju studiów w bazie.";
		}else{
			$this->view->empty = "";
		}
		$session = new Zend_Session_Namespace('identity');
    	$designation = $this->_getParam('designation');
        if(isset($session->statusdel) && $this->_getParam('statusdelete') == 'true') {
        	$this->view->delete = "Rekord \"".$designation."\" został poprawnie usunięty.";
			unset($session->statusdel);	
        }else{
        	$this->view->delete = "";
        }
    }

    public function addAction(){
    	
    	$form = new Application_Form_TypeStudy();
    	$form->appendValidationEngineFiles($this->view); 
    	$polish = BH_TranslatePL::getPolishTranslation();
  		$translate = new Zend_Translate('array', $polish, 'pl');
  		$form->setTranslator($translate);
    	
    	$form->submit->setLabel('Dodaj nowy rodzaj studiów');
		$this->view->TypeStudyForm = $form;
   	
    	$request = $this->getRequest();
    	if ($request->isPost()) {
    		if ($form->isValid($request->getPost())) {
    			$typeStudy = new Application_Model_Entities_TypeStudy();
    			$typeStudy->setDesignation( $form->getValue('designation'));
    			$typeStudyDao = new Application_Model_Dao_TypeStudy();
    			$typeStudyDao->saveTypeStudy($typeStudy);
    		
    			$this->_helper->redirector('index');		
    		}
    	}
    }
  	public function editAction(){
		 
		$form = new Application_Form_TypeStudy();
		$form->appendValidationEngineFiles($this->view);
		$polish = BH_TranslatePL::getPolishTranslation();
		$translate = new Zend_Translate('array', $polish, 'pl');
		$form->setTranslator($translate);
		 
		$form->submit->setLabel('Edytuj rodzaj studiów');
		$this->view->TypeStudyEditForm = $form;

		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
			if ($form->isValid($formData)) {
				$typeStudy = new Application_Model_Entities_TypeStudy();
				$typeStudy->setId( $form->getValue('id'));
				$typeStudy->setDesignation( $form->getValue('designation'));
				$typeStudyDao = new Application_Model_Dao_TypeStudy();
				$typeStudyDao->updateTypeStudy($typeStudy);

				$this->_helper->redirector('index');
			} else {
				$form->populate($formData);
			}
		}else{
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$typeStudyDao = new Application_Model_Dao_TypeStudy();
				$form->populate($typeStudyDao->getTypeStudyByID($id));
			}
		}
	}
public function deleteAction(){
		$session = new Zend_Session_Namespace('identity');
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del');
			if ($del == 'Tak, usuń rekord') {
				$id = $request->getPost('id');
				$designation = $request->getPost('designation');
				$typeStudy = new Application_Model_Entities_TypeStudy();
				$typeStudy->setId($id);
				$typeStudyDao = new Application_Model_Dao_TypeStudy();
				$typeStudyDao->deleteTypeStudy($typeStudy);
    			$session->statusdel = 'true';
				$this->_helper->redirector( 'index', 'typestudy', 'technical', array('statusdelete' => 'true', 'designation' => $designation));
			}else
				$this->_helper->redirector( 'index');
		}
		if($this->_getParam('jsflag') == 'false' ){
			$id = $this->_getParam('id', 0);
			if ($id > 0) {
				$typeStudyDao = new Application_Model_Dao_TypeStudy();
				$result = $typeStudyDao->getRowTypeStudy($id);
				$this->view->typeStudies = $result;
			}
		}
		
		if($this->_getParam('jsflag') == 'true' ){
			$id = $this->_getParam('id', 0);
				$typeStudy = new Application_Model_Entities_TypeStudy();
				$typeStudy->setId($id);
				$typeStudyDao = new Application_Model_Dao_TypeStudy();
				$typeStudyDao->deleteTypeStudy($typeStudy);
			//$this->_helper->redirector('index');
		}
	}
    
}
